package demo04_bean;

import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Created by ztt on 2017/9/16.
 */
@Configuration
@ComponentScan
public class Config {

    @Bean(name="emailerService")
    @Conditional(WindowsCondition.class)
    public EnBook windowsEmailerService(){
        return new EnBook();
    }

    @Bean(name="emailerService")
    @Conditional(LinuxCondition.class)
    public CnBook linuxEmailerService(){
        return new CnBook();
    }

}

class WindowsCondition implements Condition{
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }
}

class LinuxCondition implements Condition{
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Linux");  }
}